#!/usr/bin/perl -w

#########################################################################
#									#
#               ORIGINAL VERSION 2 REPOSITORY FORMAT                    # 
#               If the repository type is PV then things are stored     #
#               differently - rather than each release having a         #
#               separate file all files are stored in a single SQLite   #
#               database with the following format:                     #
#               Key             tag|relfile                             #
#               LVersion        number                                  #
#               Version         Sha1                                    #
#                                                                       #
#               There are special formats as well:                      #
#               [1]                                                     #
#               Key             tag|+++META1                            #
#               Version         UID,UNIXTIME                            #
#               [2]                                                     #
#               Key             tag|+++META2                            #
#               Version         Description                             #
#                                                                       #
#########################################################################

BEGIN {
	push @INC,$ENV{CODEMGR_LIBS} if exists($ENV{CODEMGR_LIBS});
}

use DBI;

if(! -f "release.db") {
	print STDERR "Current directory does not contact a 'release.db' file.\n";
	exit 1;
}

$dbh=DBI->connect("dbi:SQLite:dbname=release.db","","",{ sqlite_handle_binary_nulls => 1});
if(!defined($dbh)) {
	print STDERR "The 'release.db' file in not in SQLite format!\n";
	exit 1;
}

# Grab out all releases ...

$h = $dbh->selectall_hashref("SELECT * FROM taglines WHERE KEY like '%|+++META1';","key");
$h2 = $dbh->selectall_hashref("SELECT * FROM taglines WHERE KEY like '%|+++META2';","key");
@t=sort (map {my $x=$_; $x =~ s/\|\+\+\+META1$//; $x; } (keys(%$h))); 

$|=1;
if(! -f "\$release.db") {
	$dbh2=DBI->connect("dbi:SQLite:dbname=\$release.db","","",{ sqlite_handle_binary_nulls => 1});
	$dbh2->do("CREATE TABLE releases(name VARCHAR(20) PRIMARY KEY, utime INTEGER,desc VARCHAR(100), author CHAR(10));");
	$dbh2->do("COMMIT;");
} else {
	$dbh2=DBI->connect("dbi:SQLite:dbname=\$release.db","","",{ sqlite_handle_binary_nulls => 1});
}

for $crel (@t) {
	next if -f "$crel.db\$";
	print "Log: Converting tag '$crel' ...";
	my ($uid,$unixtime)=split(/,/,$h->{"$crel|+++META1"}->{version});
	my $desc=$h2->{"$crel|+++META2"}->{version};
	$dbh2->do("INSERT or REPLACE INTO releases (name,utime,desc,author) VALUES ('$crel','$unixtime','$desc','$uid');");

	# Create the database and the empty table...

	$dbh3=DBI->connect("dbi:SQLite:dbname=$crel.db\$","","",{ sqlite_handle_binary_nulls => 1});
	$dbh3->do("CREATE TABLE line(filename VARCHAR(100) PRIMARY KEY,lversion INTEGER,version CHAR(40));");

	$h3=$dbh->selectall_hashref("SELECT * FROM taglines WHERE KEY like '$crel|%';","key");
	$c=0;
	for (keys(%$h3)) {
		my $x=$_;
		$x=~s/^$crel\|//;
		next if $x eq "+++META1" || $x eq "+++META2";
		$dbh3->do("INSERT or REPLACE INTO line (filename,lversion,version) VALUES('$x','$h3->{$_}->{lversion}','$h3->{$_}->{version}');");
		$c++;

	}
	print "Done [$c files]\n";
}	
